package org.telegram.bot.exception;

import org.telegram.bot.emums.BotExceptionEnums;

/**
 * 业务异常
 *
 * @author ruoyi
 */
public final class BotApiException extends RuntimeException
{
    private static final long serialVersionUID = 1L;

    /**
     * token错误的异常信息
     */
    private static final String EXCEPTION_TOKEN_ERROR = "Error removing old webhook";
    /**
     * 错误码
     */
    private Integer code;

    /**
     * 错误提示
     */
    private String message;

    /**
     * 错误明细，内部调试错误
     *
     * 和 {@link CommonResult#getDetailMessage()} 一致的设计
     */
    private String detailMessage;

    /**
     * 空构造方法，避免反序列化问题
     */
    public BotApiException()
    {
    }

    public BotApiException(Throwable cause) {
        this.message = cause.getMessage();
    }

    public BotApiException(String message)
    {
        this.message = message;
    }

    public BotApiException(String message, Integer code)
    {
        this.message = message;
        this.code = code;
    }

    public String getDetailMessage()
    {
        return detailMessage;
    }

    @Override
    public String getMessage()
    {
        if(BotExceptionEnums.TOKEN_ERROR.getMsg().equals(message)){
            message = BotExceptionEnums.TOKEN_ERROR.getExplain();
        }else if(message.indexOf(BotExceptionEnums.ONLY_ONE.getMsg())>-1){
            message = BotExceptionEnums.ONLY_ONE.getExplain();
        }
        return message;
    }

    public Integer getCode()
    {
        return code;
    }

    public BotApiException setMessage(String message)
    {
        this.message = message;
        return this;
    }

    public BotApiException setDetailMessage(String detailMessage)
    {
        this.detailMessage = detailMessage;
        return this;
    }
}
